package com.ziyi.tiantianshuiyin.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ziyi.tiantianshuiyin.R;


/**
 * ==========================================================
 * <p>
 * 描 述 ：自定义头部VIE  公共title、
 * <p>
 * ==========================================================
 **/
public class TitleView extends FrameLayout implements View.OnClickListener {

    private TextView tvRightText;
    private TextView mTitle;
    private Context context;
    private LinearLayout ll_back;
    private ImageView iv_back;
    private ImageView iv_right;
    private RelativeLayout rl_title;
    private RelativeLayout rl;

    private OnLeftButtonClickListener mOnLeftButtonClickListener;
    private OnRightButtonClickListener mOnRightButtonClickListener;

    public interface OnLeftButtonClickListener {
        public void onClick(View button);
    }

    public interface OnRightButtonClickListener {
        public void onClick(View button);
    }

    public void setLeftButton(OnLeftButtonClickListener listener) {
        ll_back.setVisibility(View.VISIBLE);
        mOnLeftButtonClickListener = listener;
    }

    public void setLeftButton(String text, OnLeftButtonClickListener listener) {
        ll_back.setVisibility(View.VISIBLE);
        mOnLeftButtonClickListener = listener;
    }

    public void setLeftButton(int stringID, OnLeftButtonClickListener listener) {
        ll_back.setVisibility(View.VISIBLE);
        mOnLeftButtonClickListener = listener;
    }

    public void setRightButton(int stringID, OnRightButtonClickListener listener) {
        rl.setVisibility(View.VISIBLE);
        iv_right.setBackgroundResource(stringID);
        mOnRightButtonClickListener = listener;
    }

    public void removeLeftButton() {
        ll_back.setVisibility(View.INVISIBLE);
        mOnLeftButtonClickListener = null;
    }

    public void hiddenLeftButton() {
        ll_back.setVisibility(View.INVISIBLE);
    }

    public void showLeftButton() {
        ll_back.setVisibility(View.VISIBLE);
    }


    public void setRightButton(String text, OnRightButtonClickListener listener) {
        tvRightText.setText(text);
        tvRightText.setTextColor(getResources().getColor(R.color.black));
        rl.setVisibility(View.VISIBLE);
        tvRightText.setVisibility(View.VISIBLE);
        mOnRightButtonClickListener = listener;
    }

//    public void setRightButton(int stringID, OnRightButtonClickListener listener) {
//        tvRightText.setText(stringID);
//        tvRightText.setBackgroundResource(R.drawable.titleview_button_selector);
//        tvRightText.setHeight(DisplayUtils.dip2px(context, 48));
//        tvRightText.setVisibility(View.VISIBLE);
//        mOnRightButtonClickListener = listener;
//    }

    public void setRightButton(String text, int resid,
                               OnRightButtonClickListener listener) {
        tvRightText.setBackgroundResource(resid);
        tvRightText.setText(text);
        tvRightText.setVisibility(View.VISIBLE);
        mOnRightButtonClickListener = listener;
    }

    public void removeRightButton() {
        tvRightText.setText("");
        tvRightText.setVisibility(View.INVISIBLE);
        mOnRightButtonClickListener = null;
    }

    public void hiddenRightButton() {
        tvRightText.setVisibility(View.INVISIBLE);
    }

    public void showRightButton() {
        tvRightText.setVisibility(View.VISIBLE);
    }

    public void setIvBackVisibility(int visibility) {
        iv_back.setVisibility(visibility);
    }

    public TitleView(Context context) {
        this(context, null);
        this.context = context;
    }

    public TitleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.context = context;
    }

    public TitleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context = context;

        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.title_view, this, true);

        rl_title = (RelativeLayout) findViewById(R.id.rl_title);
        rl = (RelativeLayout) findViewById(R.id.rl);
        rl.setOnClickListener(this);
        //setBackgroundColor(Color.argb(240, 234, 229, 229));
        setBackgroundColor(getResources().getColor(R.color.white));
        tvRightText = (TextView) findViewById(R.id.tv_right_text);
        tvRightText.setVisibility(View.INVISIBLE);

        ll_back = (LinearLayout) findViewById(R.id.ll_back);
        ll_back.setOnClickListener(this);

        mTitle = (TextView) findViewById(R.id.title_text);
        mTitle.setVisibility(View.INVISIBLE);

        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_right = (ImageView) findViewById(R.id.iv_right);

    }

    public void setTitle(String text) {
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText(text);
    }

    public void setTitle(int stringID) {
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText(stringID);
    }

    public void setTitleColor(int color) {
        mTitle.setTextColor(color);
    }


    public void setRightText(String text) {
        tvRightText.setText(text);
    }

    public void setRightText(int stringID) {
        tvRightText.setText(stringID);
    }

    public void setRightIco(int res) {
        // R.drawable.ico_down
        Drawable drawable = context.getResources().getDrawable(res);
        // / 这一步必须要做,否则不会显示.
        drawable.setBounds(5, 0, drawable.getMinimumWidth(),
                drawable.getMinimumHeight());
        tvRightText.setCompoundDrawables(null, null, drawable, null);
    }

    public TextView getRigthButton() {
        return tvRightText;
    }


    @Override
    public void setBackgroundColor(int color) {
        rl_title.setBackgroundColor(color);
    }

    @Override
    public void setBackgroundResource(int resid) {
        rl_title.setBackgroundResource(resid);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.ll_back) {
            if (mOnLeftButtonClickListener != null)
                mOnLeftButtonClickListener.onClick(v);
        }
        if (v.getId() == R.id.rl) {
            if (mOnRightButtonClickListener != null)
            mOnRightButtonClickListener.onClick(v);
        }
    }

}
